package com.wind.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wind.entity.Dimensionality;
import com.wind.entity.Explain;
import com.wind.service.IDimensionalityService;
import com.wind.service.IExplainService;
import com.wind.util.DimToExpUtil;
import com.wind.vo.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 维度管理
 */
@RestController
@RequestMapping("/dimen")
public class DimenController {

    @Autowired
    private IDimensionalityService dimensionalityService;

    @Autowired
    private IExplainService explainService;

    @RequestMapping("/add")
    public Json add(@RequestBody String body) {
        Dimensionality dimensionality = JSON.parseObject(body, Dimensionality.class);
        if (!dimensionalityService.insert(dimensionality)) {
            return Json.fail("新增失败");
        }
        //生成分析结果组合
        List<Dimensionality> list = dimensionalityService.selectList(
                new EntityWrapper<Dimensionality>().eq("is_active", 1));
        if (list.size() > 2) {
            List<Long> idList = list.stream().map(Dimensionality::getId).collect(Collectors.toList());
            List<String> strList = new ArrayList<>();
            Collections.addAll(strList, idList.toArray(new String[idList.size()]));
            List<String> expList = DimToExpUtil.orderGroup(strList, 3);
            expList.forEach(obj -> {
                Explain explain = new Explain();
                explain.setType(1);
                explain.setDimensionalitysId(obj);
                explain.setIsActive(1);
                String[] dimIds = obj.split(",");
                String expName = "";
                for (String id : dimIds) {
                    Dimensionality dim = dimensionalityService.selectById(id);
                    expName += dim.getLogogram();
                }
                explain.setResultName(expName);
                if (!(explainService.selectCount(new EntityWrapper<Explain>()
                        .eq("dimensionalitys_id", obj)) > 0)) {
                    explainService.insert(explain);
                }
            });
        }
        return Json.succ();
    }


    @RequestMapping("/del")
    public Json del(@RequestBody String body) {
        JSONObject json = JSON.parseObject(body);
        Integer id = json.getIntValue("id");
        Dimensionality dimensionality = dimensionalityService.selectById(id);
        if (dimensionality == null) {
            return Json.fail("获取维度信息失败");
        }
        dimensionality.setIsActive(0);
        if (!dimensionalityService.updateById(dimensionality)) {
            return Json.fail("删除失败");
        }
        return Json.succ();
    }

    @RequestMapping("/update")
    public Json update(@RequestBody String body) {
        Dimensionality dimensionality = JSON.parseObject(body, Dimensionality.class);
        if (!dimensionalityService.updateById(dimensionality)) {
            return Json.fail("更新失败");
        }
        return Json.succ();
    }

    @RequestMapping("/page")
    public Json page(@RequestBody String body) {
        JSONObject json = JSON.parseObject(body);
        String name = json.getString("name");
        int current = json.getIntValue("current");
        int size = json.getIntValue("size");
        if (current == 0) current = 1;
        if (size == 0) size = 10;
        Page<Dimensionality> page = dimensionalityService.selectPage(new Page<>(current, size),
                new EntityWrapper<Dimensionality>()
                        .like("name", name));
        return Json.succ().data("page", page);
    }
}
